// pages/order/order-detail/index.js
var url = getApp().globalData.ApiUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: [],   // 订单详情
    state_desc: '',
    key: wx.getStorageSync("key"),
    pay_sn: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var key = wx.getStorageSync("key");
    this.setData({
      pay_sn: options.pay_sn,
    })
    wx.request({    // 获取订单详情
      url: url + '/mobile/index.php?act=member_order&op=order_info',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'GET',
      data: {
        key: key,
        order_id: options.order_id
      },
      success: (res) => {
        if (res.data.code == 200) {
          var arr = [];
          var list = res.data.datas.order_info;
          var payment_name = list.payment_name.replace(/[&\|\\\*^%$#@\-]/g, "");
          var pay = payment_name.replace(/[^\u4e00-\u9fa5]/gi, "");
          var promotion = list.promotion[0];
          this.setData({
            order: list,
            pay: pay,
            promotion: promotion
          })
        };
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none'
        })
      }
    })
  },
  // 去付款
  submitForm: function (e) {
    var key = wx.getStorageSync("key");
    wx.request({
      url: url + '/mobile/index.php?act=member_payment&op=applet_pay',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      dataType: 'json',
      data: {
        key: key,
        pay_sn: this.data.pay_sn
      },
      success: function (res) {
        wx.requestPayment({
          'timeStamp': res.data.datas.timeStamp,
          'nonceStr': res.data.datas.nonceStr,
          'package': res.data.datas.package,
          'signType': 'MD5',
          'paySign': res.data.datas.paySign,
          'success': function (res) {
            if (status == 2) {
              wx.navigateTo({
                url: '/pages/my-group/index?buyer_id=' + that.data.buyer_id + '&pay_sn=' + that.data.pay_sn,
              });
            }
            this.onShow();
          },
          'fail': function (res) {
            if (res.errMsg == 'requestPayment:fail cancel') {
              wx.showToast({
                title: '取消付款',
                icon: 'none',
                duration: 800
              })
            }
          }
        });
      },
      fail: function (res) {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
  // 跳转到店铺
  toStore: function(e) {
    wx.navigateTo({
      url: '/pages/store/home/index?store_id=' + e.currentTarget.dataset.store_id,
    })
  },
  // 跳转到商品详情
  toDetail: function(e) {
    wx.navigateTo({
      url: '/pages/group-detail/index?goods_id=' + e.currentTarget.dataset.goods_id,
    })
  },
  // 跳转到物流页面
  toOrderLogistics: function (e) {
    var ids = e.currentTarget.dataset.order_id;
    var img = e.currentTarget.dataset.goods_img;
    var desc = e.currentTarget.dataset.state_desc;
    var num = e.currentTarget.dataset.goods_num;
    wx.navigateTo({
      url: '/pages/order/order-logistics/index?order_id=' + ids + '&goods_image_url=' + img + '&state_desc=' + desc + '&goods_num=' + num,
    })
  },
  // 查看更多优惠
  toMoreCounps: function() {
    wx.switchTab({
      url: '/pages/discount/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})